﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PMS.Server.Entities;
using PMS.Server.IService;
using PMS.Server.Models;

namespace PMS.Server.Start.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ContractController : ControllerBase
    {
        IContractService _contract;
        public ContractController(IContractService contract)
        {
            _contract = contract;
        }

        [HttpGet("page/{key}/{start}/{end}/{index}/{size}")]
        public IActionResult GetInfo(string key, string start, string end, int index, int size)
        {
            Result<Page<ContractEntity[]>> result = new Result<Page<ContractEntity[]>>();
            try
            {
                key = (key == "none" ? "" : key);
                int total = 0;
                var datas = _contract.GetDatas(key,
                    DateTime.Parse(start), DateTime.Parse(end),
                    index, size, ref total);
                Page<ContractEntity[]> page = new Page<ContractEntity[]>();
                page.PageIndex = index;
                page.PageSize = size;
                page.TotalCount = total;
                page.Data = datas;
                result.Data = page;
            }
            catch (Exception ex)
            {
                result.State = 500;
                result.ExceptionMessage = ex.Message;
            }
            return Ok(result);
        }
    }
}
